package com.lagou.sort;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class SortReducer extends Reducer<LongWritable, NullWritable, Text, NullWritable> {

    private final Text k = new Text();
    private int rank = 1;
    @Override
    protected void reduce(LongWritable key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        // 遍历，以防相同的数字进入同一组
        for (NullWritable value : values) {
            // 拼接排名
            String content = Integer.toString(rank) + "\t" + key.toString();
            k.set(content);
            context.write(k, NullWritable.get());
            rank++;
        }
    }
}
